Hi, from different posts here and from the guide for Doors 9.6 I saw that there is no garbage collection or automatically free of the memory in Doors so the users have to deal with it on their own.
I want to make a post as a reference to put all the ways to free the memory in one place. If I am forget about something or do wrongs please correct me.
Information:
- One of the best memory free operations are closing modules and carefully usage of strings.
Memory de-allocation is not automatic for the dynamic types Skip, Array, Buffer, DB, OleAutoArgs, IPC and Stat. Repeated use of these types can consume memory and reduce performance of DOORS.
In some cases this can even lead to DOORS crashing.
To avoid such memory leaks you must explicitly de-allocate memory used by these variables. This means that you should have a delete() or destroy() function call for every create() function call in your program.
-1. Close open Modules
Folder f = current
Item i
Module mod
for i in f do
{
if(type(i) "" == "Formal")
{
mod = edit(fullName(i) "", false)
//process and add links here
save mod
close mod
}
}
0. string
There is no function for de-allocating memory used by string variables. Every new string declared or constructed in DXL is added to the "string table". The memory used by the string table is not released until the DOORS session
is terminated.
Memory being consumed by the string table is a particular problem in scripts such as Layout DXL that are run many times.
Avoid creating unnecessary strings through concatenation. The following prints out object identifiers:
Object o
for o in current Module do
{
print("Object: " identifier(o) "n")
}
This is inefficient because the new strings being added to the string table are, for example
"Object: SR 1n", "Object: SR 2n", "Object: SR 3n"
Rewriting the code as follows factors out the strings "Object: " and "n" and thus uses less space in the string table.
Object o
for o in current Module do
{
print("Object: ")
print(identifier(o))
print("n")
}
New strings being added to the string table are, for example:
view source print?
"Object: ", "n", "SR 1", "SR 2", "SR 3"
If you are doing a lot of string processing use Buffers instead of strings. If you need to convert a Buffer to a string you can use the undocumented function
string tempStringOf(Buffer)
Buffer bsz = create
bool doneOne = true
if (doneOne)
{
bsz += "\n"
}
bsz += identifier(othero)
print tempStringOf(bsz) ""
delete(bsz)
1. Regex - free the memory used by the regex in Doors 9.0+
Perm:
delete(regex)
New in Rational DOORS 9.0 this perm deletes the supplied regular expression and frees the memory used by it
Example:
Regexp line = regexp2 ".*"
while (line txt1) {
...
}
delete(line)
2.Arrays
Because DXL provides no garbage collection, it is important to delete DXL's dynamic arrays that are no longer required,
thereby freeing allocated memory.
- Arrays are one dimensional. If we want to use them as multidimensional we can use the trick
Example:
//Define an array of (for example) bool
int imax=5
int jmax=7
bool myarray[imax*jmax]
//Access for example element myarray[3][2]
int i=3
int j=2
bool mybool=myarray[i*jmax+j]
- Arrays are auto-resized but this is slow operation for automatic resizing, it is good to know the size of the array.
Example:
Array oneArray = create(1,1);
put(oneArray, "Huhuhuhu", 0,0);
put(a,23,10,10);
delete(oneArray);
Perm
void delete(Array a)
Operation
Deletes array a; stored contents are not affected
3. Skip list
Because DXL provides no garbage collection, it is important to delete skip lists that are no longer required, thereby freeing
allocated memory
Term:
void delete(Skip s)
Operation
Deletes all of skip list s. Variables that have been given as keys or data are not affected
Example:
Skip strKeys = create
put(strKeys, "literal" "", 1000)
void deleteSkip(Skip sList)
{ // Delete nested skip list and free memory
Skip s
for s in sList do {
delete s
}
delete sList
} // Delete nested skip list and free memory
4. Stat - is a data structure describing files and file-like elements like directories. These functions return information about a file.
Perm:(undocumented)
void delete( Stat s)
Example:
/************************************
isDirectory
Returns true if string parameter is a valid directory
************************************/
bool isDirectory(string dn)
{
Stat s = create dn
if (null s) return false
if (directory s)
{
delete s
return true
}
delete s
return false
}
5. Buffer
Perm:
void delete(Buffer &b)
Operation
Deletes the buffer b, and sets the variable b to null.
Example:
Buffer b = create
string s = get(htmlCtrl)
print s
b = s
set(html, b)
delete b
6. OleAutoArgs
Perm:
void delete(OleAutoArgs autoArgs)
Operation
Destroys a type OleAutoArgs variable and frees any system resources used by it. After a type OleAutoArgs variable
has been deleted with this function, it becomes invalid and cannot be used again until initialized with the create
(OleAutoArgs) function.
Example:
This example is a typical call to delete for a variable of type OleAutoArgs:
OleAutoArgs autoArgs = create
delete(autoArgs)
7. Icon
Perm:
void destroy(Icon iconName)
Operation
Frees up resources used by iconName. Use this when you destroy a dialog box; for further information, see the
destroy(dialog box) function.
Examples:
Icon ic = load("c:\\test.ico")
set(theTab, 0, ic)
// .. then on program close
destroy ic
Dinio - Thu Dec 31 06:08:04 EST 2015 |